package com.loong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.loong.dto.EmpDTO;
import com.loong.dto.LoginDTO;
import com.loong.entity.Emp;
import com.loong.entity.EmpExpr;
import com.loong.mapper.EmpExprMapper;
import com.loong.result.PageBean;
import com.loong.mapper.EmpMapper;
import com.loong.service.EmpService;
import com.loong.vo.EmpVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageBean list(EmpDTO empDTO) {
        PageHelper.startPage(empDTO.getPage(), empDTO.getPageSize());
        List<EmpVO> empList = empMapper.list(empDTO.getName(), empDTO.getGender(), empDTO.getBegin(), empDTO.getEnd());
        Page<EmpVO> empPage = (Page<EmpVO>) empList;
        return new PageBean(empPage.getTotal(), empPage.getResult());
    }

    @Override
    public List<Emp> listAll() {
        return empMapper.selectList(null);
    }

    @Override
    public Emp get(Integer id) {
        Emp emp = empMapper.selectById(id);
        QueryWrapper<EmpExpr> qw = new QueryWrapper<>();
        qw.eq("emp_id", id);
        List<EmpExpr> exprList = empExprMapper.selectList(qw);
        if(!CollectionUtils.isEmpty(exprList)) {
            emp.getExprList().addAll(exprList);
        }
        return emp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Integer> ids) {
        empMapper.deleteByIds(ids);
        QueryWrapper<EmpExpr> qw = new QueryWrapper<>();
        qw.in("emp_id", ids);
        empExprMapper.delete(qw);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insert(Emp emp) {
        empMapper.insert(emp);
        Integer empId = emp.getId();
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insert(exprList);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void update(Emp emp) {
        empMapper.updateById(emp);
        Integer empId = emp.getId();
        QueryWrapper<EmpExpr> qw = new QueryWrapper<>();
        qw.eq("emp_id", empId);
        empExprMapper.delete(qw);
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(empId));
            empExprMapper.insert(exprList);
        }
    }

    @Override
    public Emp login(LoginDTO loginDTO) {
        QueryWrapper<Emp> qw = new QueryWrapper<>();
        qw.eq("username", loginDTO.getUsername()).eq("password", loginDTO.getPassword());
        return empMapper.selectOne(qw);
    }

    @Override
    public List<Map<String, Object>> test() {
        return empMapper.listEmpAndDept();
    }
}
